<?php
/**
 * Rakuten账号
 * (model)
 */
namespace App\Models\Basics;


use Illuminate\Database\Eloquent\Model;

class RakutenAccount extends Model{

    protected $connection = 'mysql';

    protected $table = 'b_rakuten_account';

    protected $guarded = [];

    public static $_authStatus = [
        1 => '已授权',
        0 => '未授权',
    ];

    public static $_status = [
        1 => '启用',
        0 => '停用',
    ];

    const TIME_EXPIRED = 60 * 60 * 24 * 90; //三个月

    //关联平台账号
    public function plat_account(){
        return $this->belongsTo(SysPlatAccount::class, 'id');
    }

    public static function getAccount($where = [], $id = 200000){
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        return $query->where('id',$id)->first();
    }

    public static function getAccountById($id){
        return self::query()->where('id',$id)->value('account');
    }

    public static function getIdByAccount($account){
        return self::query()->where('account',$account)->value('id');
    }

    public static function options($where = [])
    {
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        return $query->pluck('account','id');
    }

    public static function optionsForApi($where = [])
    {
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        return $query->where('status',1)->get();
    }

}
